package com.liang.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liang.mall.entity.Admin;
import com.liang.mall.group.Insert;
import com.liang.mall.group.Update;
import com.liang.mall.util.ResultEntity;
import com.liang.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lin
 * @since 2021-05-07
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @ResponseBody
    @PostMapping("/login")
    public ResultEntity login(@RequestBody Admin admin, HttpSession session){

//        System.out.println("username = " + username + ", password = " + password);

      Boolean success =  adminService.login(admin.getUsername(),admin.getPassword());


      if (success){
          session.setAttribute("adminID",admin.getId());
          session.setAttribute("admin",admin);
          return ResultEntity.ok("success");
      }else {
          return ResultEntity.error(1000,"登录失败");
      }

    }


    @ResponseBody
    @PostMapping("/logout")
    public ResultEntity logout(HttpSession session){

        session.removeAttribute("adminID");
        session.removeAttribute("admin");

        return ResultEntity.ok();

    }

    @ResponseBody
    @GetMapping("/list")
    public ResultEntity<IPage<Admin>> getAdminList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){

        IPage<Admin> adminIPage = adminService.getByPage(pageNum,pageSize);

      return ResultEntity.ok(adminIPage);


    }

    @ResponseBody
    @GetMapping("/query")

    public ResultEntity<IPage<Admin>> queryByName(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize,@RequestParam String username){

        IPage<Admin> adminIPage =   adminService.queryByName(pageNum,pageSize,username);

        return ResultEntity.ok(adminIPage);

    }

    @ResponseBody
    @PostMapping("/add")
    public ResultEntity add(@RequestBody @Validated(Insert.class) Admin admin){
        System.out.println("AdminController.add");
        System.out.println(admin);
        boolean save = adminService.save(admin);

        if (save){
            return ResultEntity.ok();
        }else {
            return ResultEntity.error("添加失败");
        }

    }


    @ResponseBody
    @PostMapping("/update")
    public ResultEntity update(@RequestBody @Validated(Update.class)  Admin admin){
        System.out.println("AdminController.update");

        System.out.println(admin);



        boolean success = adminService.update(admin, new UpdateWrapper<Admin>().eq("id", admin.getId()));

        if (success){

            return ResultEntity.ok();

        }else {
            return ResultEntity.error("update失败");
        }

//        boolean save = adminService.save(admin);
//
//        if (save){
//            return ResultEntity.ok();
//        }else {

//        }



    }

    @ResponseBody
    @GetMapping("/queryOne/{id}")
    public ResultEntity<Admin> queryOne(@PathVariable("id") Integer id){
        System.out.println(id);
        Admin admin = adminService.getById(id);

        return ResultEntity.ok(admin);
    }


}

